<?php
// $Id: tools.php 460 2012-07-09 08:27:03Z tomqin@gmail.com $

defined('IN_ARESCMS') or exit('Access Denied!');

class Tools_Action extends Action_Front{

    function init(){}

    //获取zone options
    function onZoneOption(){
        $cityid = App::getCityId();
        $zoneid = array_get_int($_GET, 'pid', 0);
        $sel_id = array_get_int($_GET, 'zid', 0); //selected id;

        echo $this->_zoneOption($cityid, $zoneid, $sel_id);
    }

    function onZoneCache(){
        $cityid = App::getCityId();
        $zone = App::module('zone', '', array($cityid));
        $zone->makeCache();

        print_r($zone->getZoneList(0));
    }

    function onClearImage(){
        $where = " WHERE picture LIKE '2012/05/%'";
        $sql = "SELECT picture FROM biz_album {$where}
                UNION SELECT picture FROM biz_dish {$where}
                UNION SELECT picture FROM biz_hotel_hall {$where}
                UNION SELECT picture FROM biz_shop {$where}
                UNION SELECT picture FROM biz_shop_coupon {$where}";
        $res = Db::query($sql);
        $image = array();
        while($row = $res->fetch_array()){
            $image[] = basename($row['picture']);
        }
        $files = glob(UPLOAD_DIR.'2012/05/*.*');
        echo '<pre>';
        foreach($files as $file){
            if(!in_array(basename($file), $image)){
                echo $file."\n";
            }else{
                echo '!<font color="red">'.$file."</font>\n";
            }
        }
    }

    function onShopImage(){
        $sql = "SELECT picture FROM biz_shop";
        $res = Db::query($sql);
        $image = array();
        while($row = $res->fetch_array()){
            echo '<img src="'.thumb_url($row['picture'], 80, 56).'" />';
        }
    }

    function onPinyin(){
exit();
        $str = array_get_string($_GET, 'str');
        echo json_encode($this->_pinyin($str));
    }

    function onCheckZonePinyin(){
exit();
        $city_id = array_get_int($_GET, 'city_id', 0);
        $zone_id = array_get_int($_GET, 'zone_id', 0);
        $pinyin  = array_get_string($_GET, 'pinyin');

        echo $this->_checkZonePinyin($city_id, $zone_id, $pinyin);
    }

    function onGetWaimaiTel(){
        $shop_id  = auth_string(array_get_string($_GET, 'code'), 'DECODE');
        $shop_id  = intval($shop_id);
        $telphone = Db::getFirst("select telphone from `biz_shop` where shop_id = '{$shop_id}'");
        echo trim($telphone, '/');
    }

    function onCount(){
        $shop_id  = array_get_int($_GET, 'shop_id');
        Db::query("UPDATE `biz_shop` SET views = views + 1 where shop_id = '{$shop_id}'");
        if($id = array_get_string($_GET, 'id')){
            $count = Db::getFirst("select views from `biz_shop` where shop_id = '{$shop_id}'");
            echo "document.getElementById('$id').innerHTML = '$count';";
        }
    }

    private function _zoneOption($cityid, $zoneid = 0, $sel_id = 0){
        $cityid = intval($cityid);
        $zoneid = intval($zoneid);
        $sel_id = intval($sel_id);
        $zones = Db::getAll("SELECT zone_id, parent_id, name, pinyin, zone_type, display FROM `biz_zone` WHERE `city_id` = '{$cityid}' AND `parent_id` = '{$zoneid}' ORDER BY `ordnum`");

        $str = '';
        if($zones && is_array($zones)){
            foreach($zones as $zone){
                $sel  = ($sel_id == $zone['zone_id']) ? ' selected' : '';
                $str .= '<option value="' .$zone['zone_id']. '"' .$sel. '>' .h($zone['name']). '</option>';
            }
        }
        return $str;
    }

    private function _pinyin($str){
        Base::loadFunction('iconv');
        $str = iconv(CHARSET, 'GBK', $str);
        return gbk_to_pinyin(trim($str));
    }

    private function _checkZonePinyin($city_id, $zone_id, $pinyin){
        return DB::getFirst("select count(zone_id) from `t_zone` where `city_id` = ? and `ename` like ? and `zone_id` <> ?",
                        array(intval($city_id), $pinyin, intval($zone_id)));
    }

    function __destruct(){
    }

}
?>